﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;
using System.Web.UI.HtmlControls;

namespace Web.Contact
{
    public partial class EditContact : System.Web.UI.Page
    {
        static bool Checkchangecompany = false;
        static List<HtmlAnchor> aStepControls;
        static List<HtmlGenericControl> divStepControls;
        static string prevPage = String.Empty;
        static USER user;
        static string requestEdit = string.Empty;
        static CONTACT_DETAIL conDe;
        static ROLE r;
        static string oldOrgID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            txtPrimaryEmail.Attributes.Add("onblur", "CheckNull();");
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            user = (USER)Session["User"];
            if (user != null)
            {
                Checkchangecompany = false;
                requestEdit = Request.QueryString["id"];
                if (requestEdit != null && requestEdit.Trim() != "")
                {
                    aStepControls = new List<HtmlAnchor>();
                    aStepControls.Add(aStep1);
                    aStepControls.Add(aStep2);
                    aStepControls.Add(aStep3);
                    aStepControls.Add(aStep4);
                    aStepControls.Add(aStep5);
                    divStepControls = new List<HtmlGenericControl>();
                    divStepControls.Add(divStep1);
                    divStepControls.Add(divStep2);
                    divStepControls.Add(divStep3);
                    divStepControls.Add(divStep4);
                    divStepControls.Add(divStep5);
                    //Load toan man hinh voi ConID
                    LoadLoyalty();
                    LoadSalutationType();
                    LoadAllUser();
                    LoadStatus();
                    LoadLeadSource();

                    LoadContactInfo(Convert.ToInt64(requestEdit));
                }
            }
        }
        private void LoadLoyalty()
        {
            long? assignTo = null;
            r = RoleService.GetRoleByID((long)user.ROLE_ID);
            if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
            {
                assignTo = user.ID;
                divAssignTo.Visible = false;
            }
            ddlStarterLoyalty.DataSource = LoyaltyService.ListAll(assignTo);
            ddlStarterLoyalty.DataBind();
            ddlStarterLoyalty.Items.Insert(0, new RadComboBoxItem("Select an option", "-1"));
        }
        private void showStep(string datastep)
        {
            foreach (HtmlAnchor a in aStepControls)
            {
                if (a.Attributes["data-step"] == datastep)
                    (a.Parent as HtmlGenericControl).Attributes["class"] = "active";
                else
                    (a.Parent as HtmlGenericControl).Attributes["class"] = "";
            }
            foreach (HtmlGenericControl div in divStepControls)
            {
                if (div.Attributes["data-step"] == datastep)
                    div.Style["display"] = "block";
                else
                    div.Style["display"] = "none";
            }
        }
        private void LoadContactInfo(long ConID)
        {
            conDe = ContactService.GetContactDetailByID(ConID);
            if (conDe != null)
            {
                //Bassic Information
                ddlSalutationType.FindItemByValue(conDe.SALUTATION_ID.ToString()).Selected = true;
                txtSurname.Text = conDe.SUR_NAME;
                txtLastName.Text = conDe.NAME;
                RadComboBoxItem statusItem = ddlStatus.FindItemByValue(conDe.STATUS_ID.ToString());
                if (statusItem != null)
                {
                    ddlStatus.ClearSelection();
                    statusItem.Selected = true;
                }
                RadComboBoxItem assigntoItem = ddlAssignedTo.FindItemByValue(conDe.ASSIGN_TO_ID.ToString());
                if (assigntoItem != null)
                {
                    ddlAssignedTo.ClearSelection();
                    assigntoItem.Selected = true;
                }
                RadComboBoxItem loyaltyItem = ddlStarterLoyalty.FindItemByValue(conDe.LOYALTY_STARTER_ID.ToString());
                if (loyaltyItem != null)
                {
                    ddlStarterLoyalty.ClearSelection();
                    loyaltyItem.Selected = true;
                }
                txtCurrentLoyalty.Text = conDe.LOYALTY_NAME;
                txtOfficePhone.Text = conDe.PHONE_OFFICE;

                txtOrganizationName.Text = conDe.ORGANIZATION_NAME;
                hdntxtOrgName.Value = conDe.ORGANIZATION_NAME;
                hdntxtOrgID.Value = conDe.OrgID.ToString();
                oldOrgID = conDe.OrgID.ToString();

                txtMobilePhone.Text = conDe.PHONE_MOBILE;
                ddlLeadSource.FindItemByValue(conDe.LEAD_SOURCE_ID.ToString()).Selected = true;
                txtHomePhone.Text = conDe.PHONE_HOME;
                txtDesignation.Text = conDe.DESIGNATION;
                txtSecondaryPhone.Text = conDe.PHONE_SECONDARY;
                txtDepartment.Text = conDe.DEPARTMENT;
                txtFax.Text = conDe.FAX;
                txtPrimaryEmail.Text = conDe.PRIMARY_EMAIL;
                dpDateOfBirth.SelectedDate = conDe.DATE_OF_BIRTH;
                //Customer portal details
                dpSupportStartDate.SelectedDate = conDe.SUPPORT_START_DATE;
                dbSupportEndDate.SelectedDate = conDe.SUPPORT_END_DATE;
                //Address Details
                txtOfficeStreet.Text = conDe.OFFICE_STREET;
                txtHomeStreet.Text = conDe.HOME_STREET;
                txtOfficePOBox.Text = conDe.OFFICE_PO_BOX;
                txtHomePOBox.Text = conDe.HOME_PO_BOX;
                txtOfficeCity.Text = conDe.OFFICE_CITY;
                txtHomeCity.Text = conDe.HOME_CITY;
                txtOfficeState.Text = conDe.OFFICE_STATE;
                txtHomeState.Text = conDe.HOME_STATE;
                txtOfficeZip.Text = conDe.OFFICE_ZIP;
                txtHomeZip.Text = conDe.HOME_ZIP;
                txtOfficeCountry.Text = conDe.OFFICE_COUNTRY;
                txtHomeCountry.Text = conDe.HOME_COUNTRY;
                //Load Status Info
                if (conDe.STATUS_ID == 1)
                {
                    divContactedProcess.Style["display"] = "block";
                    //Appointment
                    rbtnAppointmentYes.Checked = (conDe.APPOINTMENT != null && (bool)conDe.APPOINTMENT) ? true : false;
                    rbtnAppointmentNo.Checked = rbtnAppointmentYes.Checked ? false : true;
                    txtAppointmentNoReason.Text = conDe.NO_APPOINTMENT_REASON;
                    divAppointmentNoReason.Style["display"] = rbtnAppointmentNo.Checked ? "block" : "none";
                    btnStep1Next.Style["display"] = rbtnAppointmentYes.Checked ? "block" : "none";
                    showStep("1");
                    //Quotation issue
                    if ((conDe.APPOINTMENT != null && (bool)conDe.APPOINTMENT) && conDe.QUOTATION_ISSUE != null)
                    {
                        aStep2.Style["display"] = (conDe.APPOINTMENT != null && (bool)conDe.APPOINTMENT) ? "block" : "none";
                        rbtnQuotationIssueYes.Checked = (conDe.QUOTATION_ISSUE != null && (bool)conDe.QUOTATION_ISSUE) ? true : false;
                        rbtnQuotationIssueno.Checked = rbtnQuotationIssueYes.Checked ? false : true;
                        btnStep2Next.Style["display"] = rbtnQuotationIssueYes.Checked ? "block" : "none";
                        ddlQINoReason.Items[0].Selected = (conDe.QUOTATION_ISSUE_IS_PENDING != null && (bool)conDe.QUOTATION_ISSUE_IS_PENDING) ? true : false;
                        ddlQINoReason.Items[1].Selected = (conDe.QUOTATION_ISSUE_IS_REJECTED != null && (bool)conDe.QUOTATION_ISSUE_IS_REJECTED) ? true : false;
                        ddlQIPendingReason.Items[0].Selected = (conDe.QUOTATION_ISSUE_IS_CALL_BACK != null && (bool)conDe.QUOTATION_ISSUE_IS_CALL_BACK) ? true : false;
                        ddlQIPendingReason.Items[1].Selected = (conDe.QUOTATION_ISSUE_IS_APPOINTMENT != null && (bool)conDe.QUOTATION_ISSUE_IS_APPOINTMENT) ? true : false;
                        txtQIRejectedReason.Text = conDe.QUOTATION_ISSUE_REJECTED_RREASON;
                        divQINoReason.Style["display"] = !rbtnQuotationIssueYes.Checked ? "block" : "none";
                        divQIPendingReason.Style["display"] = (conDe.QUOTATION_ISSUE_IS_PENDING != null && (bool)conDe.QUOTATION_ISSUE_IS_PENDING) ? "block" : "none";
                        divQIRejectedReason.Style["display"] = (conDe.QUOTATION_ISSUE_IS_REJECTED != null && (bool)conDe.QUOTATION_ISSUE_IS_REJECTED) ? "block" : "none";
                        showStep("2");
                    }
                    //Deposited Collection
                    if ((conDe.QUOTATION_ISSUE != null && (bool)conDe.QUOTATION_ISSUE) && conDe.DEPOSIT_COLLECTED != null)
                    {
                        aStep3.Style["display"] = (conDe.QUOTATION_ISSUE != null && (bool)conDe.QUOTATION_ISSUE) ? "block" : "none";
                        rbtnDCYes.Checked = (conDe.DEPOSIT_COLLECTED != null && (bool)conDe.DEPOSIT_COLLECTED) ? true : false;
                        rbtnDCNo.Checked = rbtnDCYes.Checked ? false : true;
                        txtDCNoReason.Text = conDe.DEPOSIT_COLLECTED_NO_REASON;
                        rntxtAmount.Value = conDe.DEPOSIT_COLLECTED_AMOUNT;
                        rbtnDCDIIYes.Checked = (conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) ? true : false;
                        if (conDe.DEPOSIT_INVOICE_ISSUE == null)
                        {
                            btnStep3Next.Style["display"] = "block";
                            rbtnDCDIIYes.Checked = true;
                        }
                        rbtnDCDIINo.Checked = rbtnDCDIIYes.Checked ? false : true;
                        txtDCDIINoReason.Text = conDe.NO_DEPOSIT_INVOICE_ISSUE_REASON;
                        divDCNoReason.Style["display"] = !rbtnDCYes.Checked ? "block" : "none";
                        divDCAmount.Style["display"] = rbtnDCYes.Checked ? "block" : "none";
                        divDCDII.Style["display"] = (rntxtAmount.Value != null && rntxtAmount.Value != 0) ? "block" : "none";
                        divDCDIINoReason.Style["display"] = (conDe.DEPOSIT_INVOICE_ISSUE != null && !(bool)conDe.DEPOSIT_INVOICE_ISSUE) ? "block" : "none";
                        btnStep3Next.Style["display"] = (conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) ? "block" : "none";
                        showStep("3");
                    }
                    //Process order
                    if ((conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) && (conDe.PROCESS_ORDER_IS_PENDING != null || conDe.PROCESS_ORDER_IS_COMPLETED != null))
                    {
                        aStep4.Style["display"] = (conDe.DEPOSIT_INVOICE_ISSUE != null && (bool)conDe.DEPOSIT_INVOICE_ISSUE) ? "block" : "none";
                        ddlPOStatus.Items[0].Selected = (conDe.PROCESS_ORDER_IS_PENDING != null && (bool)conDe.PROCESS_ORDER_IS_PENDING) ? true : false;
                        ddlPOStatus.Items[1].Selected = (conDe.PROCESS_ORDER_IS_COMPLETED != null && (bool)conDe.PROCESS_ORDER_IS_COMPLETED) ? true : false;
                        divPOPendingDate.Style["display"] = ddlPOStatus.Items[0].Selected ? "block" : "none";
                        dpPOPendingDate.SelectedDate = conDe.PROCESS_ORDER_PENDING_DATE;
                        divPOFullII.Style["display"] = ddlPOStatus.Items[1].Selected ? "block" : "none";
                        rbtnPOFullIIYes.Checked = conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE == null ? true : (bool)conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE;
                        rbtnPOFullIINo.Checked = rbtnPOFullIIYes.Checked ? false : true;
                        btnStep4Next.Style["display"] = ddlPOStatus.Items[1].Selected && rbtnPOFullIIYes.Checked ? "block" : "none";
                        showStep("4");
                    }
                    //Payment collection
                    if ((conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE != null && (bool)conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE) && conDe.PAYMENT_COLLECTION != null)
                    {
                        aStep5.Style["display"] = (conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE != null && (bool)conDe.PROCESS_ORDER_FULL_INVOICE_ISSUE) ? "block" : "none";
                        rbtnPaidYes.Checked = (conDe.PAYMENT_COLLECTION != null && (bool)conDe.PAYMENT_COLLECTION) ? true : false;
                        rbtnPaidNo.Checked = rbtnPaidYes.Checked ? false : true;
                        divPaidNoReason.Style["display"] = rbtnPaidNo.Checked ? "block" : "none";
                        txtPaidNoReason.Text = conDe.PAYMENT_COLLECTION_NO_REASON;
                        btnStep5Done.Style["display"] = rbtnPaidYes.Checked ? "block" : "none";
                        showStep("5");
                    }
                    //Set step clicked value
                    if (aStep2.Style["display"] == "block")
                        hdntxtNextStep1Clicked.Value = "1";
                    if (aStep3.Style["display"] == "block")
                        hdntxtNextStep2Clicked.Value = "1";
                    if (aStep4.Style["display"] == "block")
                        hdntxtNextStep3Clicked.Value = "1";
                    if (aStep5.Style["display"] == "block")
                        hdntxtNextStep4Clicked.Value = "1";
                }
            }
            else
            {
                divContent.Style["display"] = "none";
                divError.Style["display"] = "block";
            }
        }
        private void LoadAllUser()
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users != null)
            {
                foreach (USER u in users)
                {
                    ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                }
            }
        }
        private void LoadLeadSource()
        {
            ddlLeadSource.DataSource = LeadSourceService.GetAllLeadSource();
            ddlLeadSource.DataBind();
        }
        private void LoadSalutationType()
        {
            ddlSalutationType.DataSource = SalutationService.GetAllSalutatioin();
            ddlSalutationType.DataBind();
        }
        private void LoadStatus()
        {
            ddlStatus.DataSource = ContactService.GetAllContactStatus();
            ddlStatus.DataBind();
        }

        private int UpdateContact()
        {
            CONTACT contact = new CONTACT();
           
            // BASIC INFORMATION
            contact.SALUTATION_ID = Convert.ToInt64(ddlSalutationType.SelectedItem.Value);
            contact.SUR_NAME = txtSurname.Text.Trim();
            contact.NAME = txtLastName.Text.Trim();
            contact.PHONE_OFFICE = txtOfficePhone.Text.Trim();
            if (hdntxtOrgID.Value == "")
                contact.ORGANIZATION_ID = null;
            else
                contact.ORGANIZATION_ID = Convert.ToInt64(hdntxtOrgID.Value);
            contact.STATUS_ID = Convert.ToInt64(ddlStatus.SelectedValue);
            contact.PHONE_MOBILE = txtMobilePhone.Text.Trim();
            contact.LEAD_SOURCE_ID = Convert.ToInt64(ddlLeadSource.SelectedItem.Value);
            contact.PHONE_HOME = txtHomePhone.Text.Trim();
            contact.DESIGNATION = txtDesignation.Text.Trim();
            contact.PHONE_SECONDARY = txtSecondaryPhone.Text.Trim();
            contact.DEPARTMENT = txtDepartment.Text.Trim();
            contact.FAX = txtFax.Text.Trim();
            contact.PRIMARY_EMAIL = txtPrimaryEmail.Text.Trim();
            contact.DATE_OF_BIRTH = dpDateOfBirth.SelectedDate;

            if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                contact.ASSIGNED_USER_ID = user.ID;
            else
            {
                if (ddlAssignedTo.SelectedItem == null)
                    contact.ASSIGNED_USER_ID = null;
                else
                    contact.ASSIGNED_USER_ID = Convert.ToInt64(ddlAssignedTo.SelectedItem.Value);
            }
            if (ddlStarterLoyalty.SelectedValue != "-1")
            {
                contact.LOYALTY_STARTER_ID = Convert.ToInt64(ddlStarterLoyalty.SelectedValue);
                contact.LOYALTY_NAME = ddlStarterLoyalty.SelectedItem.Text;
            }
            DateTime dt = DateTime.Now;
            contact.DATE_ENTERED = conDe.DATE_ENTERED;
            contact.DATE_MODIFIED = dt;
            //  CUSTOMER PORTAL DETAILS
            contact.SUPPORT_START_DATE = dpSupportStartDate.SelectedDate;
            contact.SUPPORT_END_DATE = dbSupportEndDate.SelectedDate;
            // ADDRESS DETAILS
            contact.OFFICE_STREET = txtOfficeStreet.Text.Trim();
            contact.HOME_STREET = txtHomeStreet.Text.Trim();
            contact.OFFICE_PO_BOX = txtOfficePOBox.Text.Trim();
            contact.HOME_PO_BOX = txtHomePOBox.Text.Trim();
            contact.OFFICE_CITY = txtOfficeCity.Text.Trim();
            contact.HOME_CITY = txtHomeCity.Text.Trim();
            contact.OFFICE_STATE = txtOfficeState.Text.Trim();
            contact.HOME_STATE = txtHomeState.Text.Trim();
            contact.OFFICE_ZIP = txtOfficeZip.Text.Trim();
            contact.HOME_ZIP = txtHomeZip.Text.Trim();
            contact.OFFICE_COUNTRY = txtOfficeCountry.Text.Trim();
            contact.HOME_COUNTRY = txtHomeCountry.Text.Trim();
            //Status info
            APPOINTMENT appoint = null;
            QUOTATION_ISSUES qiss = null;
            DEPOSIT_COLLECTIONS dc = null;
            PROCESS_ORDERS po = null;
            PAYMENT_COLLECTIONS paid = null;
            if (ddlStatus.SelectedValue == "1")
            {
                appoint = new APPOINTMENT();
                appoint.APPOINTMENT1 = rbtnAppointmentYes.Checked;
                appoint.NO_REASON = rbtnAppointmentYes.Checked ? null : txtAppointmentNoReason.Text.Trim();
                contact.STATUS_FINAL_STEP = "1";
                qiss = getQuotationIssue((bool)appoint.APPOINTMENT1);
                if (qiss != null) contact.STATUS_FINAL_STEP = "2";
                dc = qiss != null ? getDepositCollection((bool)qiss.QUOTATION_ISSUE) : null;
                if (dc != null)
                {
                    contact.STATUS_FINAL_STEP = "31";
                    if (dc.DEPOSIT_INVOICE_ISSUE != null)
                        contact.STATUS_FINAL_STEP = "32";
                }
                po = dc != null ? getProcessOrder(dc.DEPOSIT_INVOICE_ISSUE == null ? false : (bool)dc.DEPOSIT_INVOICE_ISSUE) : null;
                if (po != null) contact.STATUS_FINAL_STEP = "4";
                paid = po != null ? getPaymentCollection(po.FULL_INVOICE_ISSUE == null ? false : (bool)po.FULL_INVOICE_ISSUE) : null;
                if (paid != null) contact.STATUS_FINAL_STEP = "5";
            }
            if (contact.ORGANIZATION_ID.ToString() != oldOrgID)
            {
                Checkchangecompany = true;
                return ContactService.AddNewContact(contact, appoint, qiss, dc, po, paid);
            }
            contact.ID = Convert.ToInt64(requestEdit);
            return ContactService.UpdateContact(contact, appoint, qiss, dc, po, paid);
        }
        private PAYMENT_COLLECTIONS getPaymentCollection(bool poFullIIYes)
        {
            PAYMENT_COLLECTIONS paid = null;
            if (poFullIIYes && hdntxtNextStep4Clicked.Value == "1")
            {
                paid = new PAYMENT_COLLECTIONS();
                paid.PAYMENT_COLLECTION = rbtnPaidYes.Checked;
                paid.NO_REASON = (bool)paid.PAYMENT_COLLECTION ? null : txtPaidNoReason.Text.Trim();
                paid.IS_CLOSE_CASE = paid.PAYMENT_COLLECTION;
            }
            return paid;
        }
        private PROCESS_ORDERS getProcessOrder(bool dcDIIYes)
        {
            PROCESS_ORDERS po = null;
            if (dcDIIYes && hdntxtNextStep3Clicked.Value == "1")
            {
                po = new PROCESS_ORDERS();
                po.IS_PENDING = ddlPOStatus.SelectedValue == "0" ? true : false;
                po.PENDING_DATE = (bool)po.IS_PENDING ? dpPOPendingDate.SelectedDate : null;
                po.IS_COMPLETED = ddlPOStatus.SelectedValue == "1" ? true : false;
                if ((bool)po.IS_COMPLETED)
                    po.FULL_INVOICE_ISSUE = rbtnPOFullIIYes.Checked;
            }
            return po;
        }
        private DEPOSIT_COLLECTIONS getDepositCollection(bool qissYes)
        {
            DEPOSIT_COLLECTIONS dc = null;
            if (qissYes && hdntxtNextStep2Clicked.Value == "1")
            {
                dc = new DEPOSIT_COLLECTIONS();
                dc.DEPOSIT_COLLECTED = rbtnDCYes.Checked;
                dc.NO_REASON = (bool)dc.DEPOSIT_COLLECTED ? null : txtDCNoReason.Text.Trim();
                dc.AMOUNT = (bool)dc.DEPOSIT_COLLECTED ? rntxtAmount.Value : null;
                if ((bool)dc.DEPOSIT_COLLECTED && (dc.AMOUNT != null && dc.AMOUNT.Value != 0))
                {
                    dc.DEPOSIT_INVOICE_ISSUE = rbtnDCDIIYes.Checked;
                    dc.NO_DEPOSIT_INVOICE_ISSUE_REASON = (bool)dc.DEPOSIT_INVOICE_ISSUE ? null : txtDCDIINoReason.Text.Trim();
                }
            }
            return dc;
        }
        private QUOTATION_ISSUES getQuotationIssue(bool appointment)
        {
            QUOTATION_ISSUES qiss = null;
            if (appointment && hdntxtNextStep1Clicked.Value == "1")
            {
                qiss = new QUOTATION_ISSUES();
                qiss.QUOTATION_ISSUE = rbtnQuotationIssueYes.Checked;
                if (!(bool)qiss.QUOTATION_ISSUE)
                {
                    qiss.IS_PENDING = ddlQINoReason.SelectedValue == "0" ? true : false;
                    qiss.IS_REJECTED = ddlQINoReason.SelectedValue == "1" ? true : false;
                    if ((bool)qiss.IS_REJECTED)
                    {
                        qiss.REJECTED_REASON = txtQIRejectedReason.Text.Trim();
                    }
                    if ((bool)qiss.IS_PENDING)
                    {
                        qiss.IS_CALL_BACK = ddlQIPendingReason.SelectedValue == "0" ? true : false;
                        qiss.IS_APPOINTMENT = ddlQIPendingReason.SelectedValue == "1" ? true : false;
                    }
                }
            }
            return qiss;
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int updateCon = UpdateContact();
            if (updateCon == 1)
            {
                if (Checkchangecompany == true)
                    RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Company has changed! Add new Contact successful!" + "','" + "Contacts.aspx" + "');");
                else
                    RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Update contact successful !" + "','" + "Contacts.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Update contact successful !", "Contacts.aspx");
            }
            else
                if (Checkchangecompany == true)
                    RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not add new contact !" + "');");
                else
                    RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not update this contact !" + "');");
            //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not update this contact !');", true);
        }
        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(prevPage);
        }
        protected void btn_Delete_ServerClick(object sender, EventArgs e)
        {
            int ConDel = ContactService.DeleteContact(Convert.ToInt64(requestEdit));
            if (ConDel == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Delete contact successful !" + "','" + "Contacts.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Delete contact successful !", "Contacts.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"showMessage('" + "Can not delete contact !" + "');");
            //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not delete contact !');", true);
        }
    }
}